package com.example.messangersms;

import java.util.ArrayList;
import java.util.HashMap;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
	Button btn_compose;
	Button plus;
	
	
	public static Context mainContext;
	public  static ArrayList<Message> ted = new ArrayList<Message>();
    public static HashMap<String,Message> allContacts = new HashMap<String,Message>(); 
    public static ListView listOfContacts;
    public static ArrayAdapter<Message> contactListAdapter;
    public static ArrayAdapter<Message> listfill;
	private final int REPEAT_EVENT = 30000;
	private Handler mRepeatEvent;
	 DatabaseConnection dbs;
	 private  boolean databasecall = false;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//boolean firstboot = getSharedPreferences("BOOT_PREF", MODE_PRIVATE).getBoolean("firstboot", true);
		boolean firstboot = true;
		
        if (firstboot){
        Toast.makeText(getBaseContext(),"sign in or register", Toast.LENGTH_SHORT);
          Intent intent1  = new Intent(getApplicationContext(), LoginActivity.class);
                  startActivity(intent1);
                  getSharedPreferences("BOOT_PREF", MODE_PRIVATE)
                .edit()
                .putBoolean("firstboot", false)
                .commit();
        }
       
		setContentView(R.layout.activity_main);
		mainContext = this;
		listOfContacts=(ListView)findViewById(R.id.contactsListView);
		dbs = new DatabaseConnection(mainContext);
		
		btn_compose = (Button)findViewById(R.id.btn_compose);
		plus =(Button)findViewById(R.id.mess);
		 btn_compose.setOnClickListener(new OnClickListener()
		 {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,NewMessageActivity.class);
                startActivity(intent);	
			}
			 
		 });
		 plus.setOnClickListener(new OnClickListener()
		 {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				LoginActivity.UserId = "";
				Intent intent = new Intent(MainActivity.this,LoginActivity.class);
				
                startActivity(intent);	
			}
			 
		 });


	listOfContacts.setOnItemClickListener(new OnItemClickListener()
	{
		public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
		{
			Message clickedContact = contactListAdapter.getItem(position);
			viewContact(clickedContact);
		}
  	});
	}
	private void viewContact(Message clickedContact) {
		Intent intent = new Intent(this, SmsMessaging.class);
		intent.putExtra("SENDER_ID", clickedContact.getSenderId());
		intent.putExtra("MESSAGE", clickedContact.getMessage());
		startActivity(intent);
			
		}

	
	
	@SuppressLint("NewApi")
	@Override
	public void onResume()
	{
		super.onResume();
		mRepeatEvent = new Handler();
	    mRepeatEvent.postDelayed(doRepeatEvent, REPEAT_EVENT);
		
		
	}
	
	private Runnable doRepeatEvent = new Runnable() {
		@SuppressLint("NewApi")
		public void run(){
	     if (!LoginActivity.UserId.equals(""))
	     {
			
			GetMessageTask tko = new GetMessageTask();
			tko.execute();
			mRepeatEvent.postDelayed(doRepeatEvent, REPEAT_EVENT);
	        }
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
